﻿using OrderManagement;

OrderService orderService = new OrderService();

// 模拟数据
Customer alice = new Customer(1, "Alice");
Customer bob = new Customer(2, "Bob");
Customer charlie = new Customer(3, "Charlie");
Product apple = new Product(1, "Apple");
Product banana = new Product(2, "Banana");
Product cherry = new Product(3, "Cherry");

OrderDetails appleDetails = new OrderDetails(1, apple, 10, 1.0);
OrderDetails bananaDetails = new OrderDetails(2, banana, 20, 2.0);
List<OrderDetails> orderDetails1 = new List<OrderDetails>() { appleDetails, bananaDetails };
Order order1 = new Order(1, alice, orderDetails1);
orderService.AddOrder(order1);
OrderDetails cherryDetails = new OrderDetails(1, cherry, 30, 3.0);
List<OrderDetails> orderDetails2 = new List<OrderDetails>() { cherryDetails };
Order order2 = new Order(2, bob, orderDetails2);
orderService.AddOrder(order2);
OrderDetails appleDetails2 = new OrderDetails(1, apple, 5, 1.0);
OrderDetails bananaDetails2 = new OrderDetails(2, banana, 10, 2.0);
List<OrderDetails> orderDetails3 = new List<OrderDetails>() { appleDetails2, bananaDetails2 };
Order order3 = new Order(3, charlie, orderDetails3);
orderService.AddOrder(order3);

Console.WriteLine(orderService.ToString());

// 添加订单
OrderDetails cherryDetails2 = new OrderDetails(1, cherry, 40, 3.0);
List<OrderDetails> orderDetails4 = new List<OrderDetails>() { cherryDetails2 };
Order order4 = new Order(4, alice, orderDetails4);
orderService.AddOrder(order4);
Console.WriteLine(orderService.ToString());

// 重复添加订单
try
{
    orderService.AddOrder(order4);
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

// 商品重复添加订单
OrderDetails appleDetails3 = new OrderDetails(1, apple, 10, 1.0);
OrderDetails bananaDetails3 = new OrderDetails(2, banana, 20, 2.0);
List<OrderDetails> orderDetails5 = new List<OrderDetails>() { appleDetails3, bananaDetails3 };
Order order5 = new Order(5, bob, orderDetails5);
try
{
    orderService.AddOrder(order5);
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

// 查询订单
List<Order> queryResult = orderService.QueryOrder("2");
Console.WriteLine("按照关键字查询结果：");
foreach (Order order in queryResult)
{
    Console.WriteLine(order.ToString());
}

queryResult = orderService.QueryOrder(50.0);
Console.WriteLine("按照最小金额查询结果：");
foreach (Order order in queryResult)
{
    Console.WriteLine(order.ToString());
}

// 删除订单
orderService.RemoveOrder(order5);
Console.WriteLine(orderService.ToString());

// 不存在订单删除操作
try
{
    orderService.RemoveOrder(order5);
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

// 更新订单
List<OrderDetails> orderDetails6 = new List<OrderDetails>() { cherryDetails2, appleDetails3 };
Order order6 = new Order(1, alice, orderDetails6);
orderService.UpdateOrder(order6);
Console.WriteLine(orderService.ToString());

// 不存在订单更新操作
try
{
    orderService.UpdateOrder(order5);
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

// 排序订单
orderService.SortOrders();
Console.WriteLine(orderService.ToString());

orderService.SortOrders((o1, o2) => (int)(o2.TotalAmount - o1.TotalAmount));
Console.WriteLine(orderService.ToString());